/**
 * 
 */
package com.maohi.software.tupuna.implementations;

import java.util.ArrayList;
import java.util.List;

import com.maohi.software.arii.Arii;
import com.maohi.software.arii.mvc.events.ModelEvent;
import com.maohi.software.arii.mvc.listeners.ModelListener;
import com.maohi.software.arii.mvc.listeners.UIListener;
import com.maohi.software.tupuna.Page;
import com.maohi.software.tupuna.constants.UIConstant;
import com.maohi.software.tupuna.interfaces.TupunaController;
import com.maohi.software.tupuna.interfaces.TupunaUI;
import com.maohi.software.tupuna.ui.MainUI;
import com.maohi.software.tupuna.ui.PagePane;

/**
 * @author heifara
 * 
 */
public class DefaultTupunaUI implements TupunaUI, ModelListener {

	private TupunaController controller;
	private MainUI mainUi;
	private final List<UIListener> uiListeners;

	public DefaultTupunaUI() {
		this.uiListeners = new ArrayList<>();
	}

	@Override
	public void addUIListener(UIListener aListener) {
		this.uiListeners.add(aListener);
	}

	@Override
	public void dataChanged(ModelEvent aEvent) {
	}

	@Override
	public Page getPage(String aTitle) {
		int iIndex = this.mainUi.getContentUi().indexOfTab(aTitle);
		return (Page) this.mainUi.getContentUi().getComponentAt(iIndex);
	}

	@Override
	public List<UIListener> getUIListeners() {
		return this.uiListeners;
	}

	@Override
	public Page newPage() {
		Page iPage = Arii.getInstance(PagePane.class);
		this.mainUi.getContentUi().addTab(iPage.getTitle(), (PagePane) iPage);
		return iPage;
	}

	@Override
	public void openBundle(String aBundleId) {
		// TODO
	}

	@Override
	public void openUI(TupunaController aController) {
		controller = aController;
		this.controller.getModel().addModelListener(this);

		this.mainUi = aController.getInstance(MainUI.class, UIConstant.MAIN_UI);
		this.mainUi.setVisible(true);
	}

	@Override
	public void removeUIListener(UIListener aListener) {
		uiListeners.remove(aListener);
	}
}
